{% extends "portal/manage_template.html" %}
{% load i18n %}

{% block content %}

<div class="row">
   <div class="col-lg-12">
      <div class="panel panel-default">
          <table id="schedtasks" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>taskID</th>
                    <th>任务名称</th>
                    <th>任务目标</th>
                    <th>下次运行时间</th>
                    <th>触发类型</th>
                    <th>定时参数</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {% for s in schetasks %}
                  <tr>
                      <td>{{ s.id }}</td>
                      <td>{{ s.role }}</td>
                      <td>{{ s.host }}</td>
                      <td>{{ s.next }}</td>
                      <td>{{ s.trigger_type }}</td>
                      <td>{{ s.trigger_cond }}</td>
                      <td>
                              <!--<button type="button" class="btn btn-primary btn-xs" id="btn-{{ s.id }}">暂停</button>-->
                              <button type="button" class="btn btn-danger  btn-xs">删除</button>
                      </td>
                  </tr>
                {% endfor %}
            </tbody>
            <tfoot>
                <tr>
                    <th>任务ID</th>
                    <th>任务名称</th>
                    <th>任务目标</th>
                    <th>下次运行时间</th>
                    <th>触发类型</th>
                    <th>定时参数</th>
                    <th></th>
                </tr>
            </tfoot>
        </table>
      </div>
   </div>
</div>

{% endblock %}

{% block myjs %}
<script type="text/javascript" language="javascript">
var table1;
function ps_schedtask(tid)
{
    var opstr = $("#btn-"+tid).text();
    if (opstr == "暂停") {
        $("#btn-"+tid).text("重启");
        url = "/schedule/api/1.0/cron/pause";
    }
    if (opstr == "重启") {
        $("#btn-"+tid).text("暂停");
        url = "/schedule/api/1.0/cron/resume";
    }
    postdata = {
        "id": tid
    }
    $.post(url, postdata, function (data) {
        var items = [];
        $.each(data, function (key, val) {
            items[key] = val;
        });
        if ( items['Result'] != 'OK' ) {
            alert(items['error']);
            return ;
        } else {
            location.reload();
        }
    });
}
function del_schedtask(tid)
{
    if (confirm('你确定要删除这条记录吗？')) {
        url = "/schedule/api/1.0/cron/del";
        postdata = {
            "id": tid
        }
        $.post(url, postdata, function (data) {
            var items = [];
            $.each(data, function (key, val) {
                items[key] = val;
            });
            if ( items['Result'] != 'OK' ) {
                alert(items['error']);
                return ;
            } else {
                location.reload();
            }
        });
    }
}

$(document).ready(function() {
    table1 = $('#schedtasks').DataTable({
        "columnDefs": [
            {
                "targets": [ 0 ],
                "visible": false,
            },
            {
                "searchable": false,
                "orderable":  false,
                "width":      "68",
                "targets":    6,
            },
        ],
        language: {
                "sProcessing":   "处理中...",
                "sLengthMenu":   "显示 _MENU_ 项结果",
                "sZeroRecords":  "没有匹配结果",
                "sInfo":         "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty":    "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix":  "",
                "sSearch":       "搜索:",
                "sUrl":          "",
                "sEmptyTable":     "表中数据为空",
                "sLoadingRecords": "载入中...",
                "sInfoThousands":  ",",
                "oPaginate": {
                    "sFirst":    "首页",
                    "sPrevious": "上页",
                    "sNext":     "下页",
                    "sLast":     "末页"
                },
                "oAria": {
                    "sSortAscending":  ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                },
        },
        "stateSave":   true,
    });

    $('#schedtasks tbody').on( 'click', '.btn-primary', function () {
        var data = table1.row( $(this).parents('tr') ).data();

        ps_schedtask(data[0]);
    } );

    $('#schedtasks tbody').on( 'click', '.btn-danger', function () {
        var data = table1.row( $(this).parents('tr') ).data();
        del_schedtask(data[0]);
    } );
});
</script>
{% endblock %}
